use std::fmt::format;
use std::time::Instant;

pub struct DurationX {
    now: Instant,

}

/*
耗时统计

let now = DurationX::new();

...
...

let duration = now.release();
println!("耗时：{}", duration);
*/
impl DurationX{
    pub fn new() -> DurationX {
        DurationX{
            now : Instant::now()
        }
    }

    pub fn release(&self) -> String {
        let duration = self.now.elapsed();
        let s= duration.as_secs();
        let mut ms=duration.as_millis();
        let mut us=duration.as_micros();

        us-=ms*1000;
        ms-=s as u128*1000;

        if s == 0 {
            if ms == 0 {
                format!("{} us", us)
            }else{
                format!("{} ms {} us", ms, us)
            }
        }else{
            format!("{} s {} ms {} us", s, ms, us)
        }
    }

}